package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;



    @GetMapping("/{authorId}/travels")
    public Object getTravels(TravelQueryObject qo) {
        qo.setOrderBy("t.lastUpdateTime desc");
        PageInfo<Travel> result = travelService.selectForList(qo);
        return result;
    }


    @PutMapping("/{id}")
    public Object updateUser(User user) {
        JSONResult result = new JSONResult();
        try {
            userService.updateUser(user);
            result.setObj(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败！");
        }
        return result;
    }


    @PostMapping
    public Object registe(User user) {
        JSONResult result = new JSONResult();
        try {
            userService.registe(user);
            result.setObj(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
